<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>会员登录</title>
  <link rel="stylesheet" type="text/css" href="./css/login.css">
  <script src="js/jquery-3.5.1.js" type="application/javascript"></script>
  <script src="js/js.cookie.js" type="application/javascript"></script>
  <script>
    /**
     * encodeURI和decodeURI是成对来使用的，因为浏览器的地址栏有中文字符的话，
     *可以会出现不可预期的错误，所以可以encodeURI把非英文字符转化为英文编码，
     *decodeURI可以用来把字符还原回来。
     *encodeURI方法不会对下列字符进行编码：":"、"/"、";" 和 "?"，
     *encodeURIComponent方法可以对这些字符进行编码。
     */
    $(document).ready(
            function () {
              var urlParam = window.location.search;//截取当前url中"?"后面的字符串
              urlParam = decodeURI(urlParam);//可以用来把字符还原回来
              var loc = urlParam.substring(urlParam.lastIndexOf('=') + 1,
                      urlParam.length);
              if (loc != "") {
                $("#tip").html(loc);
              }

              //检查登录cookie是否有记录
              var login_name = Cookies.get('login_name');
              var login_pwd = Cookies.get('login_pwd');
              //alert(login_name+"  "+login_pwd);
              if (login_name != null && login_pwd != null) {
                $("#username").val(login_name);
                $("#userpwd").val(login_pwd);
                $("#remember").attr("checked", 'checked');
              }
            });

    function clicksubmit() {
      var username = $("#username").val();
      var userpwd = $("#userpwd").val();

      if (username == "") {
        $("#tip_username").html("用户名不能为空");
        return;
      } else if (userpwd == "") {
        $("#tip_username").html("");
        $("#tip_userpwd").html("密码不能为空");
        return;
      } else {
        $("#tip_username").html("");
        $("#tip_userpwd").html("");
      }

      $.ajax({
        type: "post",
        dataType: "json",
        url: "LoginController",
        data: {
          "username": username,
          "userpwd": userpwd
        },
        success: function (data) {
          if (data.result == '-1') {
            $("#tip_username").html("用户名不能为空");
          } else if (data.result == '-2') {
            $("#tip_username").html("");
            $("#tip_userpwd").html("密码不能为空");
            alert("aa");
          } else if 	(data.result == '1') {

            location.href = "index";

            //alert($("#remember:checked").val());
            if ($("#remember:checked").val() == "1") {
              Cookies.set('login_name', username, {expires: 7});
              Cookies.set('login_pwd', userpwd, {expires: 7});

            } else {
              Cookies.remove('login_name');
              Cookies.remove('login_pwd');
            }
          } else {
            $("#tip_username").html("");
            $("#tip_userpwd").html("");
            $("#tip").html("用户名或密码有误，请重新登陆！！");
          }
        },
        error: function (msg) {
          $("#tip").html("系统出现问题，请稍后登陆！！！");
        }
      });
    }
  </script>
</head>
<body>
<!-- login -->
<div class="top center">
  <div class="logo center">
    <a href="index" target="_blank"><img src="./image/mistore_logo.png" alt=""></a>
  </div>
</div>
<form  action="">
  <div class="login">
    <div class="login_center">
      <div class="login_top">
        <div class="left fl">会员登录</div>
        <div class="right fr">您还不是我们的会员？<a href="register" target="_self">立即注册</a></div>
        <div class="clear"></div>
        <div class="xian center"></div>

      </div>
      <div class="login_main center">
        <span id="tip"></span>
        <div class="username">用户名:&nbsp;<input class="shurukuang" id="username" type="text" name="username" placeholder="请输入你的用户名"/><span id="tip_username"></span></div>
        <div class="username">密&nbsp;&nbsp;&nbsp;&nbsp;码:&nbsp;<input class="shurukuang" id="userpwd" type="password" name="userpwd" placeholder="请输入你的密码"/><input  id="remember" name="remember"   type="checkbox" value="1"/><span id="tip_userpwd"></span></div>
        <div class="username">
          <div class="left fl">验证码:&nbsp;<input class="yanzhengma" type="text" name="username" placeholder="请输入验证码"/></div>
          <div class="right fl"><img src="./image/yanzhengma.jpg"></div>
          <div class="clear"></div>
        </div>
      </div>
      <div class="login_submit">
        <input class="submit" type="button" name="submit" value="登录" onclick="clicksubmit()">
      </div>

    </div>
  </div>
</form>
<footer>
  <div class="copyright">简体 | 繁体 | English | 常见问题</div>
  <div class="copyright">小米公司版权所有-京ICP备10046444-<img src="./image/ghs.png" alt="">京公网安备11010802020134号-京ICP证110507号</div>

</footer>
</body>
</html>